package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Flask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @Description:
 * @Author:let_free
 * @Date:2024/6/6/11:06
 **/
@Service
public class ApiService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Flask flask;

    public String getAPI(String param)
    /**
     * @description Get请求获取flask
     * @author let_free
     * @date 2024/6/6 13:30
     * @param param 传递的flask的api  比如flask为/test  这里拼接的url就是 http://localhost:8090/test
     * @return java.lang.String
     */
    {
        String url = flask.getPort() + param;
        return restTemplate.getForObject(url, String.class);
    }

    public String postAPI(String api, Map<String, Object> mapData)
    /**
     * @description post请求flask的api
     * @author let_free
     * @date 2024/6/6 14:46
     * @param api     访问路由
     * @param mapData  需要传递的参数
     * @return java.lang.String
     */
    {
        String url = flask.getPort() + api;
        HttpHeaders headers = new HttpHeaders();
//        设置请求头JSON格式
        headers.setContentType(MediaType.APPLICATION_JSON);
//        传递请求头和数据给HttpEntity
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(mapData, headers);
//        调用post方法
        return restTemplate.postForObject(url, request, String.class);
    }
}

